#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Boot_SecurityAccess_0x27_test.py
@Time    :   2024/09/14 13:31:28
@Author  :   yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SecurityAccess_Seed_NRC = [([0x27], [0x7F, 0x27, 0x13]),
                           ([0x27, 0x11, 0x15], [0x7F, 0x27, 0x13]),
                           ([0x27, 0x11, 0xFF], [0x7F, 0x27, 0x13]),
                           ([0x27, 0x05, 0x11], [0x7F, 0x27, 0x12]),
                           ([0x27, 0x01], [0x7F, 0x27, 0x12]),
                           ([0x27, 0x05], [0x7F, 0x27, 0x12]),
                           ([0x27, 0x07], [0x7F, 0x27, 0x12])]

SecurityAccess_Key_NRC = [([0x27], [0x7F, 0x27, 0x13]),
                          ([0x27, 0x12], [0x7F, 0x27, 0x13]),
                          ([0x27, 0x12, 0xFF], [0x7F, 0x27, 0x13]),
                          ([0x27, 0x12, 0x11, 0x33, 0x22], [0x7F, 0x27, 0x13]),
                          ([0x27, 0x02], [0x7F, 0x27, 0x12]),
                          ([0x27, 0x06], [0x7F, 0x27, 0x12]),
                          ([0x27, 0x08], [0x7F, 0x27, 0x12])]


class Test_SecurityAccess_Phy(object):

    @allure.severity('normal')
    @allure.story('Tester Security Access')
    @allure.description('Test positive response of 0x27 seed in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_Seed_Programming_Phy(self, action, Driver, unlock_Boot):
        iStep = 1
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 2, iStep, False) 
        time.sleep(0.5)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        time.sleep(0.5)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        time.sleep(0.5)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False)

    @allure.severity('minor')
    @allure.description('Test NRC of 0x27 seed in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_Seed_NRC_Programming_Phy(self, action, Driver, unlock_Boot):
        seed = []
        iStep = 1
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 7F', 1, iStep, False)
        time.sleep(0.5)
        iStep, uds_rep_seed, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 2, iStep, False) 
        time.sleep(0.5)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', uds_rep_seed, 0, iStep, False)   
        time.sleep(0.5)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', uds_rep_seed, 0, iStep, False) 

        time.sleep(0.5)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['22' * 32]) , '7F 27 36', 0, iStep, False)
        time.sleep(0.5)

        for i in range(4):
            iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 
            iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False) 
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['22' * 32]) , '7F 27 24', 0, iStep, False)
            time.sleep(2)
        time.sleep(1)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False) 
        time.sleep(2)

        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False) 
        seed = uds_rep[2:]

        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False) 


    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SecurityAccess_Seed_NRC]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x27 seed in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SecurityAccess_Seed_NRC, ids=ids)
    def test_SecurityAccess_Seed_NRC12_13_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)   

    @pytest.mark.smoke
    @allure.severity('normal')
    @allure.description('Test positive response of 0x27 key in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_Key_Programming_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 2, iStep, False) 
        logging.info('wait 4s')
        time.sleep(4)
        for i in range(2):
            iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 
            logging.info('wait 4s')
            time.sleep(4)
        seed = []
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False) 
        seed = uds_rep[2:]
        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False) 
        Driver['CAN'].set_signal_IGN(2)
        time.sleep(1)
        Driver['CAN'].set_signal_IGN(0)
        time.sleep(1)
        Driver['CAN'].set_signal_IGN(2)
        time.sleep(1)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False) 
        if uds_rep[0:6] != [0x67, 0x11, 0x00, 0x00, 0x00, 0x00]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
            logging.error(f'correct response:[0x67,0x11,0x00,0x00,0x00,0x00], actual response:[{actuRespStr}]')
        with assume:assert (uds_rep[0:6] == [0x67, 0x11, 0x00,0x00, 0x00, 0x00] and size == 18)

    @pytest.mark.repeat(1)
    @allure.severity('minor')
    @allure.description('Test NRC of 0x27 key in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_Key_NRC_Programming_Phy(self, action, Driver, unlock_Boot):
        seed = []
        iStep = 1
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 12 11 11', '7F 27 13', 2, iStep, False)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11 00 00 00 00', 2, iStep, True) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 24', 0, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 24', 0, iStep, False) 

        with allure.step(f"Step{iStep}:Reset the system with power"):
            Driver['power'].set_vltage(0)
            time.sleep(2)
            Driver['power'].set_vltage(12)
            time.sleep(5)
            iStep += 1
        # Test NRC35 of key of 0x27
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 2, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 35', 0, iStep, False) 
        # Test NRC35 of key of 0x27
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 35', 0, iStep, False) 
        # Test NRC36 of key of 0x27
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 36', 0, iStep, False) 

        # Test NRC37 of key of 0x27
        for i in range(4):
            iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 
            iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False) 
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 24', 0, iStep, False)    
            time.sleep(2)

        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 
        time.sleep(1)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 24', 0, iStep, False)    
        time.sleep(2)
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False) 

        # 安全访问延时溢出后FAC减1，FAC=2，后再请求invalid key，FAC=3，响应NRC36
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False) 
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 36', 0, iStep, False) 


    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SecurityAccess_Key_NRC]
    @pytest.mark.repeat(1)
    @allure.severity('minor')
    @allure.description('Test NRC of 0x27 key in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SecurityAccess_Key_NRC, ids=ids)
    def test_SecurityAccess_Key_NRC12_13_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_rep, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)      

    @allure.severity('minor')
    @allure.description('Test FAC will not be lost after ECU is restarted (reset)  in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_FAC_Continue_Accumulation_After_Reset_Programming_Phy(self, action, Driver, programming_session, unlock_Boot):
        # test ECU重启（复位）后不会丢失FAC
        # 上电、复位后，ECU首先检查各个安全等级的FAC，如果FAC小于3，ECU无需执行安全访问延时

        with allure.step("Step1:Request:0x27,0x11, Response:0x67,0x11"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:2] != [0x67, 0x11]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x67,0x11], actual response:[{actuRespStr}]')
            with assume:
                assert (uds_rep[0:2] == [0x67, 0x11] and size == 18)
        with allure.step("Step2:Request:0x27,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12, Response:0x7F,0x27,0x24"):
            uds_rep, size = Driver['UDS'].req_and_res_can(
                [0x27, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12], 34)
            if uds_rep[0:3] != [0x7F, 0x27, 0x35]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x35], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x35]

        with allure.step("Step3:Request:0x11,0x01, Response:0x51,0x03"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x11, 0x01], 2)
            if uds_rep[0:2] != [0x51, 0x01]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x51,0x01], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:2] == [0x51, 0x01]
            time.sleep(3)

        with allure.step("Step4:Request:0x10 0x03, Response:0x50,0x03"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x03], 2)
            if uds_rep[0:2] != [0x50, 0x03]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x50,0x03], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:2] == [0x50, 0x03]

        with allure.step("Step4:Request:0x31,0x01, 0x02, 0x03, Response:0x71,0x01, 0x02, 0x03"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x31,0x01, 0x02, 0x03], 4)
            if uds_rep[0:4] != [0x71,0x01, 0x02, 0x03]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x50,0x03], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:4] == [0x71,0x01, 0x02, 0x03]

        with allure.step("Step4:Request:0x10 0x02, Response:0x50,0x02"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x02], 2)
            if uds_rep[0:2] != [0x50, 0x02]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x50,0x02], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:2] == [0x50, 0x02]
            time.sleep(1)
        with allure.step("Step5:Request:0x27,0x11, Response:0x67,0x11"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:2] != [0x67, 0x11]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x67,0x11], actual response:[{actuRespStr}]')
            with assume:
                assert (uds_rep[0:2] == [0x67, 0x11] and size == 18)
        with allure.step("Step6:Request:0x27,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12, Response:0x7F,0x27,0x24"):
            uds_rep, size = Driver['UDS'].req_and_res_can(
                [0x27, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12], 34)
            if uds_rep[0:3] != [0x7F, 0x27, 0x35]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x35], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x35]

        with allure.step("Step7:Request:0x27,0x11, Response:0x67,0x11"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:2] != [0x67, 0x11]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x67,0x11], actual response:[{actuRespStr}]')
            with assume:
                assert (uds_rep[0:2] == [0x67, 0x11] and size == 18)
        with allure.step("Step8:Request:0x27,0x12,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, Response:0x7F,0x27,0x36"):
            uds_rep, size = Driver['UDS'].req_and_res_can(
                [0x27, 0x12, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12], 34)
            if uds_rep[0:3] != [0x7F, 0x27, 0x36]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x36], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x36]
        with allure.step("Step9:Request:0x27,0x11, Response:0x7F,0x27,0x35"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:3] != [0x7F, 0x27, 0x37]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x37], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x37]

    @allure.severity('minor')
    @allure.description('Test FAC will not be lost after ECU is restarted (powered down)  in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_FAC_Continue_Accumulation_After_Powerdown_Programming_Phy(self, action, Driver, programming_unlock, unlock_Boot):
        # test ECU重启（下上电）后不会丢失FAC
        # 上电、复位后，ECU首先检查各个安全等级的FAC，如果FAC小于3，ECU无需执行安全访问延时

        with allure.step("Step1:Request:0x27,0x11, Response:0x67,0x11"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:2] != [0x67, 0x11]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x67,0x11], actual response:[{actuRespStr}]')
            with assume:
                assert (uds_rep[0:2] == [0x67, 0x11] and size == 18)
        with allure.step("Step2:Request:0x27,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12, Response:0x7F,0x27,0x24"):
            uds_rep, size = Driver['UDS'].req_and_res_can(
                [0x27, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12], 34)
            if uds_rep[0:3] != [0x7F, 0x27, 0x35]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x35], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x35]

        with allure.step("Step3:Request:0x27,0x11, Response:0x67,0x11"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:2] != [0x67, 0x11]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x67,0x11], actual response:[{actuRespStr}]')
            with assume:
                assert (uds_rep[0:2] == [0x67, 0x11] and size == 18)
        with allure.step("Step4:Request:0x27,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12,0x11,0x12, Response:0x7F,0x27,0x24"):
            uds_rep, size = Driver['UDS'].req_and_res_can(
                [0x27, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12], 34)
            if uds_rep[0:3] != [0x7F, 0x27, 0x35]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x35], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x35]

        with allure.step("Step5:Power off then power on"):
            Driver['power'].set_vltage(0)
            time.sleep(3)

            Driver['power'].set_vltage(12)
            time.sleep(3)
        with allure.step("Step6:Request:0x10 0x03, Response:0x50,0x03"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x03], 2)
            if uds_rep[0:2] != [0x50, 0x03]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x50,0x03], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:2] == [0x50, 0x03]

        with allure.step("Step6:Request:0x31, 0x01, 0x02, 0x03, Response:0x71, 0x01, 0x02, 0x03"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x31, 0x01, 0x02, 0x03], 4)
            if uds_rep[0:4] != [0x71, 0x01, 0x02, 0x03]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x50,0x03], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:4] == [0x71, 0x01, 0x02, 0x03]
        
        with allure.step("Step6:Request:0x10 0x02, Response:0x50,0x02"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x02], 2)
            if uds_rep[0:2] != [0x50, 0x02]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x50,0x02], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:2] == [0x50, 0x02]
            time.sleep(1)

        with allure.step("Step7:Request:0x27,0x11, Response:0x67,0x11"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:2] != [0x67, 0x11]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x67,0x11], actual response:[{actuRespStr}]')
            with assume:
                assert (uds_rep[0:2] == [0x67, 0x11] and size == 18)
        with allure.step("Step8:Request:0x27,0x12,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, Response:0x7F,0x27,0x36"):
            uds_rep, size = Driver['UDS'].req_and_res_can(
                [0x27, 0x12, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12, 0x11, 0x12], 34)
            if uds_rep[0:3] != [0x7F, 0x27, 0x36]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x36], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x36]
        with allure.step("Step9:Request:0x27,0x11, Response:0x7F,0x27,0x37"):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x27, 0x11], 2)
            if uds_rep[0:3] != [0x7F, 0x27, 0x37]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(
                    f'correct response:[0x7F,0x27,0x37], actual response:[{actuRespStr}]')
            with assume:
                assert uds_rep[0:3] == [0x7F, 0x27, 0x37]

    @allure.severity('minor')
    @allure.description('Test ECU will be executed session jump, and "seed key" sequence will be reset in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_sequence_after_session_jump_Programming_Phy(self, action, Driver, programming_unlock, unlock_Boot):
        # Test ECU执行会话跳转，“Seed—Key” 序列将被重置
        seed = []
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        seed = uds_resp[2:]
        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '7F 27 24', 0, iStep, False)
  

    @allure.severity('minor')
    @allure.description('Test ECU will be powered on again,"seed key" sequence will be reset in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_sequence_after_poweron_again_Programming_Phy(self, action, Driver, programming_unlock, unlock_Boot):
        # Test ECU被重新上电，“Seed—Key” 序列将被重置
        seed = []
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        seed = uds_resp[2:]
        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False)

        # ECU被重新上电
        with allure.step(f"Step{iStep}:Power off then power on"):
            Driver['power'].set_vltage(0)
            time.sleep(3)
            Driver['power'].set_vltage(12)
            time.sleep(8)
            iStep += 1

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '7F 27 24', 0, iStep, False)

    @allure.severity('minor')
    @allure.description('Test ECU will be reset, "seed key" sequence will be reset in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_sequence_after_reset_Programming_Phy(self, action, Driver, programming_unlock, unlock_Boot):
        # Test ECU被复位，“Seed—Key” 序列将被重置
        seed = []
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        seed = uds_resp[2:]
        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False)
        # 复位
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01', '51 01', 0, iStep, False)
        time.sleep(2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '7F 27 24', 0, iStep, False)


    @allure.severity('minor')
    @allure.description('Test under delay response of 0x27 key then reset ECU in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_under_delay_then_reset_Programming_Phy(self, action, Driver, programming_unlock):
        # Test ECU 在delay过程中 复位, 安全访问请求,收到负响应，有访问延时（重新计时10s---9月9号邮件回复）
        # 上电、复位后，ECU首先检查各个安全等级的FAC，如果FAC大于3，应该执行安全访问延时（延时时间为10s），并将FAC赋值为3
        seed = []
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['22' * 32]) , '7F 27 35', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 35', 0, iStep, False)
        time.sleep(0.5)
        # Test NRC36 of key of 0x27
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['33' * 32]) , '7F 27 36', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False)
        time.sleep(2)
        # 复位
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01', '51 01', 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        time.sleep(1)
        # Test NRC37 of key of 0x27
        for i in range(4):
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['10' * 32]) , '7F 27 24', 0, iStep, False)
            time.sleep(2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False)
        time.sleep(2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        seed = uds_resp[2:]
        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False)

    @allure.severity('minor')
    @allure.description('Test under delay response of 0x27 key then powerreset ECU in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SecurityAccess_under_delay_then_powerreset_Programming_Phy(self, action, Driver, programming_unlock):
        # Test ECU 在delay过程中 重新上电, 安全访问请求,收到负响应，有访问延时（重新计时10s---9月9号邮件回复）
        # 上电、复位后，ECU首先检查各个安全等级的FAC，如果FAC大于3，应该执行安全访问延时（延时时间为10s），并将FAC赋值为3
        seed = []
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['22' * 32]) , '7F 27 35', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['11' * 32]) , '7F 27 35', 0, iStep, False)
        time.sleep(0.5)
        # Test NRC36 of key of 0x27
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['33' * 32]) , '7F 27 36', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False)
        time.sleep(2)

        # ECU被重新上电
        with allure.step(f"Step{iStep}:Power off then power on"):
            Driver['power'].set_vltage(0)
            time.sleep(1)
            Driver['power'].set_vltage(12)
            time.sleep(3)
            iStep += 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        time.sleep(1)

        # Test NRC37 of key of 0x27
        for i in range(4):
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '7F 27 37', 0, iStep, False)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 12 ' + ' '.join(['10' * 32]) , '7F 27 24', 0, iStep, False)
            time.sleep(2)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '27 11', '67 11', 0, iStep, False)
        seed = uds_resp[2:]
        key = Driver['CAN'].calcKey(seed, level=3)
        send_key = [0x27, 0x12] + key
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, '67 12', 0, iStep, False)
